/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.module_main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.commonlibs.conatants.NumberCode;
import com.ldc.commonlibs.ui.VMFragmentBase;
import com.ldc.commonlibs.utils.ViewModelUtil;
import com.ldc.module_main.adaapter.FlowsAdapter;
import com.ldc.module_main.databinding.MainFragmentFlowMeBinding;
import com.ldc.module_main.model.FlowsModel;
import com.ldc.module_main.viewmodel.MainViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;


public class FlowMeFragment extends VMFragmentBase<MainFragmentFlowMeBinding, MainViewModel> {
    private FlowsAdapter flowsAdapter;
    private int offset = 0;

    public static FlowMeFragment newInstance() {
        Bundle args = new Bundle();
        FlowMeFragment fragment = new FlowMeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getFlowsModel().removeObserver(this::getFlowsModel);
    }

    @Override
    protected void main(@Nullable Bundle savedInstanceState) {
        initView();
        initFlowsAdapter();
        initData();
    }

    @Override
    protected MainFragmentFlowMeBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return MainFragmentFlowMeBinding.inflate(inflater, container, false);
    }

    @Override
    protected MainViewModel initViewModel() {
        return ViewModelUtil.create(this, MainViewModel.class);
    }

    private void initView() {
        viewBinding.commonRv.refreshView.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                offset += 1;
                queryFlows();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                offset = 0;
                queryFlows();
            }
        });
    }

    private void initFlowsAdapter() {
        flowsAdapter = new FlowsAdapter();
        viewBinding.commonRv.dataList.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        viewBinding.commonRv.dataList.setHasFixedSize(true);
        viewBinding.commonRv.dataList.setAdapter(flowsAdapter);
    }


    private void initData() {
        viewModel.getFlowsModel().observeForever(this::getFlowsModel);
        queryFlows();
    }

    private void queryFlows() {
        showLoadingView();
        viewModel.flows("32953014", offset);
    }

    private void getFlowsModel(FlowsModel data) {
        showContentView();
        viewBinding.commonRv.refreshView.finishRefresh();
        viewBinding.commonRv.refreshView.finishLoadMore();
        if (data == null || data.getCode() != NumberCode.SUCCESS_CODE) {
            return;
        }
        if (offset == 0) {
            flowsAdapter.addNewData(data.getFollow());
        } else {
            flowsAdapter.addData(data.getFollow());
        }
        viewBinding.commonRv.refreshView.setNoMoreData(!data.isMore());
    }
}
